package com.spstudio.iforward.front.action;

import java.util.Map;
import java.util.logging.Logger;

import javax.annotation.PostConstruct;
import javax.enterprise.inject.Model;
import javax.faces.context.FacesContext;
import javax.inject.Inject;

import com.spstudio.iforward.front.Story;
import com.spstudio.iforward.front.interfaces.IStorySessionBeanRemote;
import com.spstudio.iforward.front.qualifier.StorySessionBeanRemoteQualifier;

@Model
public class SpecialStoryAction {
	
	@Inject @StorySessionBeanRemoteQualifier IStorySessionBeanRemote storySessionBeanRemote;
	
	private Story story;
	
	private Logger log = Logger.getLogger("StoryAction");

	@PostConstruct
	public void construct(){
		Map varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        Object itemId = varMap.get("id");
        if (itemId != null){
        	try{
        		int id = Integer.parseInt((String)itemId);
        		story = storySessionBeanRemote.loadSingleStory(id);
        	}catch(Exception e){
        		log.warning("one remote client request a non-integer item id.");
        	}
        }
        if (story == null){
        	story = storySessionBeanRemote.loadLatestStory();
        }
	}

	public Story getStory() {
		return story;
	}

	public void setStory(Story story) {
		this.story = story;
	}
}
